package ua.com.infinite.mobile.plains;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import ua.com.infinite.mobile.db.DBAdapter;
import ua.com.infinite.mobile.utils.SharedPreferencesUtils;

public class User {

	public static String IS_LOGIN = "is_login";
	public static String USER_ID = "user_id";
	public static String USER_BALLANCE = "balance";
	public static String USER_EMAIL = "user_email";
	public static String USER_FIRST_NAME = "first_name";
	public static String USER_LASR_NAME = "last_name";
	public static String USER_SESSION = "session";
	public static String USER_PHONE = "user_phone";
	public static String USER_AVATAR = "avatar";
	public static String USER_ACCOUNTS = "socials";

	public static boolean isLogin = false;

	private static DBAdapter adapter;

	private int id;
	private String session;
	private String avatar;
	private String firstName;
	private String lastName;
	private String ballance;
	private String phone;
	private String email;
	private String[] accounts;
	private String date;

	private static User instance = null;

	public static User getInstance(Context context) {
		adapter = new DBAdapter(context);
		if (instance == null) {
			if (isLogin = isLogin(context)) {
				instance = create(context, getLoginedId(context));
			} else {
				instance = new User();
			}
		}
		return instance;
	}

	private User() {
		isLogin = false;
	}

	private User(int id, String session, String avatar, String firstName, String lastName,
			String ballance, String phone, String email, String[] accaunts) {
		this();
		this.setId(id);
		this.setSession(session);
		this.setAvatar(avatar);
		this.firstName = firstName;
		this.lastName = lastName;
		this.ballance = ballance;
		this.setPhone(phone);
		this.setEmail(email);
		this.setAccounts(accaunts);
		// this.date = date;
	}

	public static void logOff(Context context) {
		SharedPreferencesUtils.logoff(context);
		instance = null;
	}

	public static void logIn(Context context, JSONObject object) {
		SharedPreferencesUtils.login(context, object);
		isLogin = true;
		adapter.createOrUpdate(object);
		instance = create(object);
	}

	private static User create(Context context, int id) {
		isLogin = true;
		User user = null;
		user = create(adapter.getUser(id));
		return user;
	}

	private static User create(JSONObject object) {
		User user = null ;
		try {
			JSONArray accounts_array = object.getJSONArray(USER_ACCOUNTS);
			String[] accounts = new String[accounts_array.length()];
			if(accounts_array.length()!=0){
				for(int i = 0; i< accounts_array.length();i++){
					accounts[i] = accounts_array.getString(i);
				}
			}
			user = new User(object.getInt(USER_ID),
					object.getString(USER_SESSION),
					object.getString(USER_AVATAR),
					object.getString(USER_FIRST_NAME),
					object.getString(USER_LASR_NAME),
					object.getString(USER_BALLANCE),
					object.getString(USER_PHONE),
					object.getString(USER_EMAIL),
					accounts);
					//object.getString(USER_ACCOUNTS).split("\\,"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return user;
	}

	public static boolean isLogin(Context context) {
		return SharedPreferencesUtils.isLogin(context);
	}

	public static int getLoginedId(Context context) {
		return Integer.valueOf(SharedPreferencesUtils.getPreference(context,
				true, USER_ID));
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getName() {
		return firstName + " " + lastName;
	}

	public String getBallance() {
		return ballance;
	}

	public void setBallance(String ballance) {
		this.ballance = ballance;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getSession() {
		return session;
	}

	public void setSession(String session) {
		this.session = session;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String[] getAccounts() {
		return accounts;
	}

	public void setAccounts(String[] accounts) {
		this.accounts = accounts;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

}
